package com.oyjd.business.sys.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.oyjd.bean.vo.MenuVO;
import com.oyjd.bean.vo.SysOperateVO;

/**
 * 
 * 业务工具方法
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
public class SysUtil {

	/**
	 * 获取角色 roleId->roleName
	 */
	public static Map<String, String> role = new HashMap<String, String>();
	/**
	 * 获取角色和对应的操作链接 roleId->List[link]
	 */
	public static Map<String, List<String>> roleToLinks = new HashMap<String, List<String>>();
	/**
	 * 获取角色和对应的菜单 roleId->JSONArray
	 */
	public static Map<String, JSONArray> roleToTreeMenu = new HashMap<String, JSONArray>();
	/**
	 * 获取角色和对应的平展菜单 roleId->List[MenuVO]
	 */
	public static Map<String, List<MenuVO>> roleToFlatMenu = new HashMap<String, List<MenuVO>>();
	/**
	 * 获取角色和对应的操作模块 roleId->List[op]
	 */
	public static Map<String, List<SysOperateVO>> roleToModuleOp = new HashMap<String, List<SysOperateVO>>();
	/**
	 * 获取权限和对应的菜单 menuId->MenuVO
	 */
	public static Map<String, MenuVO> menus = new HashMap<String, MenuVO>();

	/**
	 * 迭代一个节点的所有父节点
	 * 
	 * @param allMenus
	 * @param aimMenus
	 * @param menuId
	 */
	public static void doParent(List<MenuVO> allMenus, List<MenuVO> aimMenus, String menuId) {
		for (int i = allMenus.size() - 1; i >= 0; i--) {
			MenuVO vo = allMenus.get(i);
			if (menuId.equals(vo.getMenuId())) {
				if (!aimMenus.contains(vo)) {
					aimMenus.add(vo);
					if (null != vo.getPid()) {
						doParent(allMenus, aimMenus, vo.getPid());
					}
				}
				break;
			}
		}
	}

	/**
	 * 获取一个节点下所有子节点形成的JSONArray,给easyui 树用
	 * 
	 * @param sou
	 * @param parentId
	 * @return
	 */
	public static JSONArray getMenuLevelByPid(List<MenuVO> sou, String id) {
		JSONArray result = new JSONArray();
		for (MenuVO q : sou) {
			if (id.equals(q.getPid())) {
				JSONObject jobj = new JSONObject();
				jobj.put("id", q.getMenuId());
				jobj.put("text", q.getName());
				JSONObject att = new JSONObject();
				att.put("pid", q.getPid());
				att.put("link", q.getLink());
				jobj.put("attributes", att);
				JSONArray tmp = getMenuLevelByPid(sou, q.getMenuId());
				if (tmp.size() > 0) {
					jobj.put("state", "closed");
					jobj.put("children", tmp);
				}
				result.add(jobj);
			}
		}
		return result;
	}

}
